// domain/model/Book.java
package com.library.bookservice.domain.model;

import java.time.LocalDate;
import java.util.Objects;

/**
 * 图书领域实体
 * 包含图书的核心业务逻辑和规则
 */
public class Book {
    private Long id;
    private String isbn;
    private String title;
    private String author;
    private String genre;
    private LocalDate publicationDate;
    private boolean available;
    private Integer totalCopies;
    private Integer availableCopies;

    // 默认构造函数
    public Book() {}

    /**
     * 创建新图书的构造函数
     * @param isbn ISBN号
     * @param title 书名
     * @param author 作者
     * @param genre 分类
     * @param publicationDate 出版日期
     * @param totalCopies 总数量
     */
    public Book(String isbn, String title, String author, String genre,
                LocalDate publicationDate, Integer totalCopies) {
        this.isbn = isbn;
        this.title = title;
        this.author = author;
        this.genre = genre;
        this.publicationDate = publicationDate;
        this.totalCopies = totalCopies;
        this.availableCopies = totalCopies;
        this.available = totalCopies > 0;
    }

    /**
     * 检查图书是否可以借阅
     * @return true表示可以借阅，false表示不可借阅
     */
    public boolean canBorrow() {
        return available && availableCopies > 0;
    }

    /**
     * 借阅图书
     * 减少可用数量，更新可用状态
     */
    public void borrowBook() {
        if (!canBorrow()) {
            throw new IllegalStateException("图书不可借阅");
        }
        this.availableCopies--;
        this.available = this.availableCopies > 0;
    }

    /**
     * 归还图书
     * 增加可用数量，更新可用状态
     */
    public void returnBook() {
        if (this.availableCopies >= this.totalCopies) {
            throw new IllegalStateException("归还数量不能超过总数量");
        }
        this.availableCopies++;
        this.available = true;
    }

    // Getter和Setter方法
    public Long getId() { return id; }
    public void setId(Long id) { this.id = id; }

    public String getIsbn() { return isbn; }
    public void setIsbn(String isbn) { this.isbn = isbn; }

    public String getTitle() { return title; }
    public void setTitle(String title) { this.title = title; }

    public String getAuthor() { return author; }
    public void setAuthor(String author) { this.author = author; }

    public String getGenre() { return genre; }
    public void setGenre(String genre) { this.genre = genre; }

    public LocalDate getPublicationDate() { return publicationDate; }
    public void setPublicationDate(LocalDate publicationDate) { this.publicationDate = publicationDate; }

    public boolean isAvailable() { return available; }
    public void setAvailable(boolean available) { this.available = available; }

    public Integer getTotalCopies() { return totalCopies; }
    public void setTotalCopies(Integer totalCopies) { this.totalCopies = totalCopies; }

    public Integer getAvailableCopies() { return availableCopies; }
    public void setAvailableCopies(Integer availableCopies) { this.availableCopies = availableCopies; }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Book book = (Book) o;
        return Objects.equals(isbn, book.isbn);
    }

    @Override
    public int hashCode() {
        return Objects.hash(isbn);
    }
}